package com.flower.oss.adapter;

import com.flower.oss.entity.FileInfo;
import com.flower.oss.adapter.StorageAdapter;
import com.flower.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * @author lavender
 * @version 1.0
 * @date 2024-07-11 18:36
 */

public class MinioStorageAdapter implements StorageAdapter {

    @Resource
    private MinioUtil minioUtil;


    @Override
    @SneakyThrows
    public void createBucket(String bucketName) {
        minioUtil.createBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile multipartFile, String bucket, String objectName) {
        minioUtil.createBucket(bucket);
        if(objectName != null){
            minioUtil.uploadFile(multipartFile.getInputStream(),bucket,objectName + "/" + multipartFile.getName());
        }else {
            minioUtil.uploadFile(multipartFile.getInputStream(),bucket,multipartFile.getName());
        }

    }

    @Override
    @SneakyThrows
    public List<String> getAllBuckets() {
        return minioUtil.getAllBuckets();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return minioUtil.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return minioUtil.downLoad(bucket,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        minioUtil.deleteBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void deleteObject(String bucket, String objectName) {
        minioUtil.deleteObject(bucket,objectName);
    }
}
